import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/company_entity.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'change_company_controller.dart';

class ChangeCompanyPage extends StatelessWidget {
  final controller = Get.put(ChangeCompanyController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      resizeToAvoidBottomInset: false,
      appBar: CustomAppBar(
        context: context,
        title: "选择用油客户",
        actions: [
          Container(
            width: 153.w,
            margin: EdgeInsets.only(left: 5.w, top: 6.w, right: 16.w, bottom: 6.w),
            child: TextButtonWithIcon(
              onPressed: () {
                controller.searchCompany();
              },
              icon: Image.asset("images/ic_search_gray.png"),
              label: Text(
                "请输入客户名称",
                style: TextStyle(color: Colours.color_cfcfd5, fontSize: 14.sp),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
              textIconPadding: 10.w,
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colours.color_background),
                shape: MaterialStateProperty.all(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(28.w),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
      body: GetBuilder<ChangeCompanyController>(
        builder: (controller) {
          return MultiStateWidget(
            child: Container(
              margin: EdgeInsets.only(top: 12.w),
              child: ListView.separated(
                shrinkWrap: true,
                physics: BouncingScrollPhysics(),
                itemBuilder: (context, index) {
                  return buildItemWidget(index);
                },
                separatorBuilder: (context, index) {
                  return Divider(
                    height: 1.w,
                    thickness: 1.w,
                    color: Colours.color_background,
                  );
                },
                itemCount: controller.companyList.length,
              ),
            ),
            state: controller.multiState,
            onRetry: () {
              controller.onRetry();
            },
          );
        },
      ),
    );
  }

  Widget buildItemWidget(int index) {
    CompanyEntity companyEntity = controller.companyList[index];
    return Material(
      child: InkWell(
        onTap: () {
          controller.backResult(companyEntity.id ?? "", companyEntity.name ?? "");
        },
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
          child: Text(
            companyEntity.name ?? "",
            style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
          ),
        ),
      ),
    );
  }
}
